@layout ProfileLayout
@page "/admin/profile/password/"
@inject HttpClient _http
@inject IStringLocalizer<Resource> _localizer
@inject IToaster _toaster

<PageTitle Title="@_localizer["change-password"]" />

<h1 class="section-title">@_localizer["change-password"]</h1>
<div class="section-content -half">
   @if (Author != null)
   {
      <EditForm Model="@Model" OnValidSubmit="SavePassword">
         <DataAnnotationsValidator />
         <div class="form-item">
            <label class="form-label">@_localizer["password"]</label>
            <input class="form-control" type="password" @bind="Model.Password" />
            <ValidationMessage For="@(() => Model.Password)"></ValidationMessage>
         </div>
         <div class="form-item">
            <label class="form-label">@_localizer["confirm-password"]</label>
            <input class="form-control" type="password" @bind="Model.PasswordConfirm" />
            <ValidationMessage For="@(() => Model.PasswordConfirm)"></ValidationMessage>
         </div>
         <div class="form-item">
            <button type="submit" class="btn btn-blogifier px-5">@_localizer["save"]</button>
         </div>
      </EditForm>
   }
</div>
<ToasterComponent @ref="Toaster" />

@code {
   protected Author Author { get; set; }
   protected RegisterModel Model { get; set; }
   protected ToasterComponent Toaster;

   protected override async Task OnInitializedAsync()
   {
       await Load();
   }

   protected async Task Load()
   {
       Author = await _http.GetFromJsonAsync<Author>("api/author/getcurrent");
       Model = new RegisterModel
       {
           Name = Author.DisplayName,
           Email = Author.Email
       };
   }

   protected async Task SavePassword()
   {
       Toaster.Toast(await _http.PutAsJsonAsync<RegisterModel>("api/author/changepassword", Model));
       await Load();
   }
}
